<template>
	<view class="">
		<nav-bar title="地址管理"></nav-bar>
		<van-address-list @select="selectAddress" v-model="chosenAddressId" :list="list" default-tag-text="默认"
			@add="onAdd" @edit="onEdit" />
	</view>
</template>

<script>
	import {
		ref
	} from 'vue';
	const Address = uniCloud.importObject('address')
	import Storage from '@/untils/storage.js'
	export default {
		setup() {
			return {
				chosenAddressId: ref("")
			};
		},
		data() {
			return {
				list: []
			}
		},
		onLoad() {
			this.getAddressList()
		},
		methods: {
			onAdd() {
				uni.navigateTo({
					url: "/pages/user/address/add"
				})
			},
			getAddressList() {
				Address.list(Storage.userinfo._id).then(res => {
					let list = res.data;
					for (let i = 0; i < list.length; i++) {
						list[i].id = list[i]._id;
						if (list[i].is_default) {
							this.chosenAddressId = list[i]._id
						}
					}
					this.list = list
				})
			},
			onEdit(e) {
				uni.navigateTo({
					url: "/pages/user/address/edit?id="+e.id
				})
			},
			selectAddress(e) {
				this.chosenAddressId = e.id
				Address.setDefault(e.user_id, e.id).then(res => {
					this.$store.commit('setDefaultAddress',e)
				})
			}
		}
	};
</script>

<style>
</style>
